package com.example.checkqufuface.Adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.checkqufuface.Utils.SelfToastUtil;
import com.example.checkqufuface.Customer.BottomPeopleMessageDialog;
import com.example.checkqufuface.Customer.BottomTicketAndReturnDialog;
import com.example.checkqufuface.R;
import com.example.checkqufuface.RetrufitHelp.BaseSubscriber;
import com.example.checkqufuface.RetrufitHelp.ExceptionHandle;
import com.example.checkqufuface.RetrufitHelp.RetrofitClient;
import com.example.checkqufuface.bean.QufuQueryAppOrderDetailResultBean;
import com.example.checkqufuface.bean.QufuQueryCertificateRequsetBean;
import com.example.checkqufuface.bean.QufuQueryCertificateResultBean;
import com.example.checkqufuface.bean.QufuQueryTicketCheckResultBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ticketImforMessageShowAdapter extends RecyclerView.Adapter<ticketImforMessageShowAdapter.ViewHolder> {

    private Context context;
    private List<QufuQueryAppOrderDetailResultBean.DataBean.AppOrderDetailResultListBean> appOrderDetailResultList;
    private String baseUrl;
    private Map<String, String> headers;


    public ticketImforMessageShowAdapter(Context context, List<QufuQueryAppOrderDetailResultBean.DataBean.AppOrderDetailResultListBean> appOrderDetailResultList, String baseUrl, Map<String, String> headers) {
        this.context = context;
        this.appOrderDetailResultList = appOrderDetailResultList;
        this.baseUrl = baseUrl;
        this.headers = headers;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_ticket_list_message, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {

        holder.tvName.setText(appOrderDetailResultList.get(position).getScenicTicketName());
        holder.tvData.setText(appOrderDetailResultList.get(position).getArriveDT());
        holder.tvMoney.setText(appOrderDetailResultList.get(position).getSalePrice() + "");

        int saleSum = appOrderDetailResultList.get(position).getSaleSum();
        String tackSum = appOrderDetailResultList.get(position).getTackSum();
        if (tackSum == null) {
            tackSum = "0";
        }
        String checkSum = appOrderDetailResultList.get(position).getCheckSum();
        if (checkSum == null) {
            checkSum = "0";
        }
        String returnSum = appOrderDetailResultList.get(position).getReturnSum();
        if (returnSum == null) {
            returnSum = "0";
        }
        holder.tvState.setText("购" + saleSum + "     取" + tackSum + "    检"
                + checkSum + "      退" + returnSum);

        holder.tvPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //实名制信息
                getPeopleMessage(appOrderDetailResultList.get(position).getOrderDetailId());
            }
        });
        holder.tvInOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取检票明细
                checkAndReturn(appOrderDetailResultList.get(position).getOrderDetailId());

            }
        });

    }

    private void checkAndReturn(long orderDetailId) {
        QufuQueryCertificateRequsetBean certificateRequsetBean = new QufuQueryCertificateRequsetBean();
        certificateRequsetBean.setOrderDetailId(orderDetailId);

        RetrofitClient.getInstance(context, baseUrl, headers).queryTicketCheck(certificateRequsetBean, new BaseSubscriber<QufuQueryTicketCheckResultBean>(context) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                SelfToastUtil.getInstance().show(e.message);
            }

            @Override
            public void onNext(QufuQueryTicketCheckResultBean qufuQueryTicketCheckResultBean) {
                String code = qufuQueryTicketCheckResultBean.getCode();
                if (code.equals("200")) {
                    List<QufuQueryTicketCheckResultBean.DataBean.CheckResultListBean> checkResultList = qufuQueryTicketCheckResultBean.getData().getCheckResultList();
                    List<QufuQueryTicketCheckResultBean.DataBean.ReturnResultListBean> returnResultList = qufuQueryTicketCheckResultBean.getData().getReturnResultList();
                    List<QufuQueryTicketCheckResultBean.DataBean.TackResultListBean> tackResultList = qufuQueryTicketCheckResultBean.getData().getTackResultList();

                    if (checkResultList == null) {
                        checkResultList = new ArrayList<>();
                    }

                    if (returnResultList == null) {
                        returnResultList = new ArrayList<>();
                    }

                    if (tackResultList == null) {
                        tackResultList = new ArrayList<>();
                    }

                    if ((checkResultList.size() + returnResultList.size() + tackResultList.size())> 0) {   // 检票，退票，取票
                        BottomTicketAndReturnDialog bottomTicketAndReturnDialog = new BottomTicketAndReturnDialog(context,checkResultList,returnResultList,tackResultList);
                        bottomTicketAndReturnDialog.show();
                    } else {
                        SelfToastUtil.getInstance().show("该票取检票明细暂无内容，请确认查询内容");
                    }

                } else {
                    SelfToastUtil.getInstance().show(qufuQueryTicketCheckResultBean.getMessage());
                }
            }
        });

    }

    private void getPeopleMessage(long orderDetailId) {
        QufuQueryCertificateRequsetBean certificateRequsetBean = new QufuQueryCertificateRequsetBean();
        certificateRequsetBean.setOrderDetailId(orderDetailId);
        RetrofitClient.getInstance(context, baseUrl, headers).queryCertificate(certificateRequsetBean, new BaseSubscriber<QufuQueryCertificateResultBean>(context) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                SelfToastUtil.getInstance().show(e.message);
            }

            @Override
            public void onNext(QufuQueryCertificateResultBean qufuQueryCertificateResultBean) {
                String code = qufuQueryCertificateResultBean.getCode();
                if (code.equals("200")) {

                    List<QufuQueryCertificateResultBean.DataBean> data = qufuQueryCertificateResultBean.getData();
                    if (data.size() > 0) {
                        BottomPeopleMessageDialog bottomPeopleMessageDialog = new BottomPeopleMessageDialog(context,data,baseUrl);
                        bottomPeopleMessageDialog.show();

                    } else {
                        SelfToastUtil.getInstance().show("该票实名制信息列表为空，请确认查询内容");
                    }

                } else {
                    SelfToastUtil.getInstance().show(qufuQueryCertificateResultBean.getMessage());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return appOrderDetailResultList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final TextView tvData;
        private final TextView tvMoney;
        private final TextView tvState;
        private final TextView tvPeople;
        private final TextView tvInOut;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvName = itemView.findViewById(R.id.tv_message_name);
            tvData = itemView.findViewById(R.id.tv_message_data);
            tvMoney = itemView.findViewById(R.id.tv_message_money);
            tvState = itemView.findViewById(R.id.tv_message_state);
            tvPeople = itemView.findViewById(R.id.tv_message_people_message);
            tvInOut = itemView.findViewById(R.id.tv_message_know);


            tvPeople.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
            tvPeople.getPaint().setAntiAlias(true);//抗锯齿

            tvInOut.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
            tvInOut.getPaint().setAntiAlias(true);//抗锯齿
        }
    }
}
